package com.aa.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aa.CallLogData;
import com.aa.CallLogDetail;
import com.aa.R;
import com.aa.CallLogDetail.ViewHolder;

public class CallLogDetailAdapter extends BaseAdapter{
	
	
	public class ViewHolder{
	    public TextView dateView;
	    public TextView durationView;
	    public TextView typeView;
   }
	private ArrayList<CallLogData> mList;
	private Context mContext;
	public CallLogDetailAdapter(ArrayList<CallLogData> pList,Context pContext){
		mList = pList;
		mContext = pContext;
	}
	@Override
	public int getCount() {
		if (mList == null) {
			return 0;
		}
		
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		if (mList == null) {
			return null;
		}
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CallLogData _CallLogData = (CallLogData)mList.get(position);
		ViewHolder _ViewHolder;
		if (convertView == null) {
			_ViewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.calllogdetail_item, null);
			_ViewHolder.typeView = ((TextView)convertView.findViewById(R.id.calllogdetail_item_type));
			_ViewHolder.dateView = ((TextView)convertView.findViewById(R.id.calllogdetail_item_date));
			_ViewHolder.durationView = ((TextView)convertView.findViewById(R.id.calllogdetail_item_duration));
			convertView.setTag(_ViewHolder);
		}else {
			_ViewHolder = (ViewHolder)convertView.getTag();
		}
		 Long _Long = Long.valueOf(Long.parseLong(_CallLogData.date));
		 _ViewHolder.dateView.setText(DateUtils.getRelativeTimeSpanString(_Long.longValue(), System.currentTimeMillis(), 60000L,DateUtils.FORMAT_ABBREV_RELATIVE ));
		 if (_CallLogData.duration != 0) {
			 _ViewHolder.durationView.setText(String.valueOf(_CallLogData.duration) + mContext.getString(R.string.calllogdetail_sec));
		}else {
			_ViewHolder.durationView.setText(R.string.calllog_call_wjt);
		}
		 
		 if (_CallLogData.type != null && !_CallLogData.type.equals("")) {
			if (Integer.parseInt(_CallLogData.type) == 1 || Integer.parseInt(_CallLogData.type) == 3){
				_ViewHolder.typeView.setText(R.string.calllog_type_in);
			}else {
				_ViewHolder.typeView.setText(R.string.calllog_type_out);
			}
		}
		return convertView;
	}
	
}

